package com.cat.slidingWindow;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/length-of-longest-subarray-with-at-most-k-frequency/description/
 * @create 2025/9/17 12:21
 * @since JDK17
 */

public class Solution37 {
    public int maxSubarrayLength(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int n = nums.length, ans = 0;
        for (int l = 0, r = 0; r < n; r++) {
            map.merge(nums[r], 1, Integer::sum);
            while (map.get(nums[r]) > k) {
                map.merge(nums[l++], -1, Integer::sum);
            }
            ans = Math.max(ans, r - l + 1);
        }

        return ans;
    }
}
